package org.come.until;


import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import org.come.Frame.ZhuFrame;
import org.come.model.Lingbao;
import org.come.socket.Agreement;
import org.come.socket.SendMessageUntil;

import com.tool.role.RoleData;

/**
 * 玩家数据判断
 * @author Administrator
 *
 */
public class UserData {
	/**
	 * 获取带小数点的后1位
	 */
	public static String xiaoshu(double xs) {
		String is=xs+"";
		String[] v=is.split("\\.");
		if (v.length==1||v[1].length()==1) {
			return is;
		}
		v[1]=v[1].substring(0,1);is=v[0]+"."+v[1];
		return is;
	}
	/**
	 * 获取带小数点的后3位
	 */
	public static String xiaoshu3(double xs) {
		String is=xs+"";
		String[] v=is.split("\\.");
		if (v.length==1||v[1].length()<=3) {
			return is;
		}
		v[1]=v[1].substring(0,3);is=v[0]+"."+v[1];
		return is;
	}
	/**
	 *返回一个字符串k=v v值
	 */
	public static String vfh(String v,String b){
		try {
			return v.split(b+"=")[1].split("\\|")[0];

		} catch (Exception e) {
			// TODO: handle exception
		}
		return "";
	}
	/**
	 * 将一个字段加入或者删除到另一个字段
	 * 主字段            预备拼接字段
	 * 0表示全字段匹配  1表示=替换 2加 3减 4删除部分匹配字段的值
	 * 5添加大数
	 */
	public static String Splice(String v,String b,int type){
		boolean s=true;
		boolean s1=false;
		if (type==2||type==3||type==5) {s1=true;}
		List<String> jihe=new ArrayList<>();
		if (v==null) {v="";}
		String[] vs=v.split("\\|");
		for (int i = 0; i < vs.length; i++) {
			if (type==0) {
				if (!vs[i].equals(b)) {
					jihe.add(vs[i]);
				} else {
					s=false;
				}
			}else {
				String[] vs1=vs[i].split("=");
				String[] vs2=b.split("=");
				if (vs1[0].equals(vs2[0])) {
					if (type==1) {
						jihe.add(b);
					}else if (type==2) {
						s1=false;
						try {
							double x1=Double.parseDouble(vs1[1]);
							double x2=Double.parseDouble(vs2[1]);
							x1=x1+x2;
							if (x1%1==0) {
								jihe.add(vs1[0]+"="+((int)x1));
							}else {
								jihe.add(vs1[0]+"="+(x1));
							}
						} catch (NumberFormatException e) {
							// 如果不是数字，则直接替换
							jihe.add(b);
						}
					}else if (type==3){
						s1=false;
						try {
							double x1=Double.parseDouble(vs1[1]);
							double x2=Double.parseDouble(vs2[1]);
							x1=x1-x2;
							if (x1%1==0) {
								jihe.add(vs1[0]+"="+((int)x1));
							}else {
								jihe.add(vs1[0]+"="+(x1));
							}
						} catch (NumberFormatException e) {
							// 如果不是数字，则直接替换
							jihe.add(b);
						}
					}else if (type==5) {
						s1=false;
						try {
							double x1=Double.parseDouble(vs1[1]);
							double x2=Double.parseDouble(vs2[1]);
							if (x2>x1) {
								x1=x2;
							}
							if (x1%1==0) {
								jihe.add(vs1[0]+"="+((int)x1));
							}else {
								jihe.add(vs1[0]+"="+(x1));
							}
						} catch (NumberFormatException e) {
							// 如果不是数字，则直接替换
							jihe.add(b);
						}
					}
				}else {
					jihe.add(vs[i]);
				}
			}

		}
		if (s) {
			if (type==0) {
				jihe.add(b);
			}
		}
		if (s1) {
			jihe.add(b);
		}
		StringBuffer genggai=new StringBuffer();
		for (int i = 0; i < jihe.size(); i++) {
			if (!"".equals(genggai.toString())) {
				genggai.append("|"+jihe.get(i));
			}else {
				genggai.append(jihe.get(i));
			}
		}
		return genggai.toString();
	}
	/**判断是否有足够银两 够扣除  返回true*/
	public static boolean uptael(long tael){
		if (tael<=0) {return false;}
		long shengyu=RoleData.getRoleData().getLoginResult().getGold().longValue()-tael;
		if (tael>0&&shengyu>=0) {
			RoleData.getRoleData().getLoginResult().setGold(new BigDecimal(shengyu));
			//发送服务器
			Senduptael(tael+"");
			ZhuFrame.getZhuJpanel().addPrompt2("扣除了#G"+tael+"#Y两!");
			return true;
		}
		ZhuFrame.getZhuJpanel().addPrompt("#R注意：#Y您的银两不足！");
		return false;
	}

	/**判断是否有足够银两 够扣除  返回true*/
	public static boolean uptaelByType(long tael){
		if (tael<=0) {return false;}
		long shengyu=RoleData.getRoleData().getLoginResult().getGold().longValue()-tael;
		if (shengyu>=0) {
			RoleData.getRoleData().getLoginResult().setGold(new BigDecimal(shengyu));
			ZhuFrame.getZhuJpanel().addPrompt2("扣除了#G"+tael+"#Y两!");
			return true;
		}
		ZhuFrame.getZhuJpanel().addPrompt("#R注意：#Y您的银两不足！");
		return false;
	}

	/**
	 * 将当前银两发送给服务器
	 * @return
	 */
	public static void Senduptael(String tael){
		String sendMes = Agreement.Deductiontael(tael);
		SendMessageUntil.toServer(sendMes);
	}

	/**发送服务器更新灵宝*/
	public static void upling(Lingbao lingbao) {

		String sendMes = Agreement.UpdateLing(GsonUtil.getGsonUtil().getgson().toJson(lingbao));
		SendMessageUntil.toServer(sendMes);
	}

	/**
	 * 随机生成指定精确度的小数
	 * @return
	 */
	public static double getDouble(double min,double max,int type){
		StringBuffer a=new StringBuffer();for (int i = 0; i < type; i++) {a.append("0");}
		max-=min;max=max/4*Util.random.nextInt(5);
		DecimalFormat df=new DecimalFormat("#."+a);
		double b=Double.valueOf(df.format(Util.random.nextDouble()*max+min));
		return b;
	}
	/**
	 * 随机抗性属性
	 * 根据等级设定最低最大值
	 */
	public static String kangxing(int lvl){
		StringBuffer buffer=new StringBuffer();
		String[] v={"抗物理","抗震慑","抗风","抗雷","抗水","抗火","抗混乱","抗昏睡","抗封印","抗中毒","抗遗忘","抗鬼火"};
		buffer.append(suiji(v)+"=");
		int fudu=(lvl-1)/40+1;
		double a=getDouble((fudu-1)*2+0.1, fudu*2+0.1, 1);
		if (a>10) {a=10;}
		buffer.append(a);
		return buffer.toString();
	}
	/**
	 * 获取字符串数组中随机一个数
	 */
	public static String suiji(String[] v){return v[Util.random.nextInt(v.length)];}


	/**大话币文字转换*/
	public static String getMS(BigDecimal money){
		StringBuffer buffer=new StringBuffer();
		long jg=money.longValue();
		jg/=1000000;
		if (jg<=0) {
			return "";
		}
		int x0=(int) ((jg/10000)%10);
		int x1=(int) ((jg/1000)%10);
		int x2=(int) ((jg/100)%10);
		int x3=(int) ((jg/10)%10);
		int x4=(int) (jg%10);
		String value=getS(x0);
		if (value!=null) {
			buffer.append(value).append("百");
		}
		value=getS(x1);
		if (value!=null) {
			if (x1!=1) {
				buffer.append(value);
			}
			buffer.append("十");
		}
		value=getS(x2);
		if (value!=null) {
			buffer.append(value);
		}
		if (buffer.length()!=0) {
			buffer.append("亿");
		}
		value=getS(x3);
		if (value!=null) {
			buffer.append(value);
			buffer.append("千");
		}
		value=getS(x4);
		if (value!=null) {
			buffer.append(value);
			buffer.append("百");
		}
		if (!buffer.substring(buffer.length()-1, buffer.length()).equals("亿")) {
			buffer.append("万");
		}
		return buffer.toString();
	}
	public static String getS(int x){
		switch (x) {
			case 1:return "一";
			case 2:return "二";
			case 3:return "三";
			case 4:return "四";
			case 5:return "五";
			case 6:return "六";
			case 7:return "七";
			case 8:return "八";
			case 9:return "九";
		}
		return null;
	}
}

